package com.lineage.server.command.executor;

import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Item;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1CreateItem implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1CreateItem.class);
  
  public static L1CommandExecutor getInstance() {
    return new L1CreateItem();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      StringTokenizer st = new StringTokenizer(arg);
      String nameid = st.nextToken();
      long count = 1L;
      if (st.hasMoreTokens())
        count = Long.parseLong(st.nextToken()); 
      int enchant = 0;
      if (st.hasMoreTokens())
        enchant = Integer.parseInt(st.nextToken()); 
      int itemid = 0;
      try {
        itemid = Integer.parseInt(nameid);
      } catch (NumberFormatException e) {
        itemid = ItemTable.get().findItemIdByNameWithoutSpace(nameid);
        if (itemid == 0) {
          pc.sendPackets((ServerBasePacket)new S_SystemMessage("没有找到条件吻合的物品。"));
          return;
        } 
      } 
      L1Item temp = ItemTable.get().getTemplate(itemid);
      if (temp != null) {
        if (temp.isStackable()) {
          L1ItemInstance item = ItemTable.get().createItem(itemid);
          item.setEnchantLevel(0);
          item.setCount(count);
          item.setIdentified(false);
          if (pc.getInventory().checkAddItem(item, count) == 0) {
            pc.getInventory().storeItem(item);
            pc.sendPackets(
                (ServerBasePacket)new S_ServerMessage(403, String.valueOf(String.valueOf(item.getLogName())) + "(ID:" + itemid + ")"));
          } 
        } else {
          if (count > 10L) {
            pc.sendPackets((ServerBasePacket)new S_SystemMessage("不可以堆叠的物品一次创造数量禁止超过10"));
            return;
          } 
          L1ItemInstance item = null;
          int createCount = 0;
          while (createCount < count) {
            item = ItemTable.get().createItem(itemid);
            item.setEnchantLevel(enchant);
            item.setIdentified(false);
            if (pc.getInventory().checkAddItem(item, 1L) != 0)
              break; 
            pc.getInventory().storeItem(item);
            createCount++;
          } 
          if (createCount > 0)
            pc.sendPackets(
                (ServerBasePacket)new S_ServerMessage(403, String.valueOf(String.valueOf(item.getLogName())) + "(ID:" + itemid + ")")); 
        } 
      } else {
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("指定ID不存在"));
      } 
    } catch (Exception e2) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
}
